// _GraphPropertiesAppearance.cpp : implementation file
//

#include "stdafx.h"
#include "FlexGraph.h"
#include "_GraphPropertiesAppearance.h"
#include "afxdialogex.h"
#include "_GraphControl.h"
#include "FlexGraphInterface.h"
#include "_CsvIO.h"

// _GraphPropertiesAppearance dialog

IMPLEMENT_DYNAMIC(_GraphPropertiesAppearance, CDialogEx)

_GraphPropertiesAppearance::_GraphPropertiesAppearance(CWnd* pParent /*=NULL*/)
    : CDialogEx(_GraphPropertiesAppearance::IDD, pParent)
{
}

_GraphPropertiesAppearance::~_GraphPropertiesAppearance()
{
}

void _GraphPropertiesAppearance::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_GRAPH_LIST, m_ListGraph);
    DDX_Control(pDX, IDC_CHECK_VISIBLE, m_CheckVisible);
    DDX_Control(pDX, IDC_ColorCtrlPoint, m_CtrlPointColor);
    DDX_Control(pDX, IDC_ColorCtrlLine, m_CtrlLineColor);
    DDX_Control(pDX, IDC_COMBO_POINT_SIZE, m_ComboPointSize);
    DDX_Control(pDX, IDC_CHECK_SCATTER, m_CheckScatter);
    DDX_Control(pDX, IDC_EDIT_GRAPHTITLE, m_EditGraphTitle);
}


BEGIN_MESSAGE_MAP(_GraphPropertiesAppearance, CDialogEx)
    ON_WM_SHOWWINDOW()
    ON_LBN_SELCHANGE(IDC_GRAPH_LIST, &_GraphPropertiesAppearance::OnSelchangeGraphList)
    ON_BN_CLICKED(IDC_ColorCtrlLine, &_GraphPropertiesAppearance::OnClickedColorctrlline)
    ON_BN_CLICKED(IDC_ColorCtrlPoint, &_GraphPropertiesAppearance::OnClickedColorctrlpoint)
    ON_BN_CLICKED(IDC_CHECK_VISIBLE, &_GraphPropertiesAppearance::OnClickedCheckVisible)
    ON_CBN_SELCHANGE(IDC_COMBO_POINT_SIZE, &_GraphPropertiesAppearance::OnSelchangeComboPointSize)
    ON_BN_CLICKED(IDC_BUTTON_REMOVE, &_GraphPropertiesAppearance::OnBnClickedButtonRemove)
    ON_BN_CLICKED(IDC_CHECK_SCATTER, &_GraphPropertiesAppearance::OnBnClickedCheckScatter)
    ON_EN_CHANGE(IDC_EDIT_GRAPHTITLE, &_GraphPropertiesAppearance::OnEnChangeEditGraphtitle)
    ON_BN_CLICKED(IDC_BUTTON_EXPORT, &_GraphPropertiesAppearance::OnBnClickedButtonExport)
END_MESSAGE_MAP()


// _GraphPropertiesAppearance message handlers


void _GraphPropertiesAppearance::updateGraphList()
{
    while(m_ListGraph.GetCount())
    {
        m_ListGraph.DeleteString(0);
    }

    map<int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();

    for(auto Iterator = pGraphs->begin(); Iterator != pGraphs->end(); Iterator++)
    {
        _GraphData* pGraph = &Iterator->second;
        m_ListGraph.AddString(pGraph->m_Title);
    }
}
void _GraphPropertiesAppearance::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialogEx::OnShowWindow(bShow, nStatus);
    EnableThemeDialogTexture(this->GetSafeHwnd(), ETDT_ENABLETAB);

    if(bShow)
    {
        updateGraphList();
        m_ListGraph.SetCurSel(0);
        OnSelchangeGraphList();
        CString Temp;
        //Temp.Format(_T("%.1f"), m_pGraphControl->m_PointSize);
        m_ComboPointSize.SelectString(0, Temp);
        m_CtrlLineColor.EnableOtherButton(_T("Other"), FALSE, TRUE);
        m_CtrlPointColor.EnableOtherButton(_T("Other"), FALSE, TRUE);
    }
    else
    {
        while(m_ListGraph.GetCount() != 0)
        {
            m_ListGraph.DeleteString(0);
        }
    }
}


void _GraphPropertiesAppearance::OnSelchangeGraphList()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        m_CtrlLineColor.SetColor(pGraph->m_LineColor);
        m_CtrlPointColor.SetColor(pGraph->m_PointColor);
        m_CheckVisible.SetCheck(pGraph->m_Visible);
        CString Temp;
        Temp.Format(_T("%.1lf"), pGraph->m_PointSize);
        m_ComboPointSize.SelectString(0, Temp);
        m_CheckScatter.SetCheck(!pGraph->m_DrawLine);
        m_EditGraphTitle.SetWindowTextW(pGraph->m_Title);
        m_CheckScatter.EnableWindow(TRUE);
        m_CheckVisible.EnableWindow(TRUE);
        m_EditGraphTitle.EnableWindow(TRUE);
        m_CtrlLineColor.EnableWindow(TRUE);
        m_CtrlPointColor.EnableWindow(TRUE);
        m_ComboPointSize.EnableWindow(TRUE);
    }
    else
    {
        m_CheckScatter.SetCheck(FALSE);
        m_CheckVisible.SetCheck(FALSE);
        m_CtrlPointColor.SetColor(0);
        m_CtrlLineColor.SetColor(0);
        m_EditGraphTitle.SetWindowTextW(L"");
        m_ComboPointSize.SetCurSel(0);
        m_CheckScatter.EnableWindow(FALSE);
        m_CheckVisible.EnableWindow(FALSE);
        m_EditGraphTitle.EnableWindow(FALSE);
        m_CtrlLineColor.EnableWindow(FALSE);
        m_CtrlPointColor.EnableWindow(FALSE);
        m_ComboPointSize.EnableWindow(FALSE);
    }
}


void _GraphPropertiesAppearance::OnClickedColorctrlline()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        pGraph->m_LineColor = m_CtrlLineColor.GetColor();
        FG_requestRedraw(m_GraphControlHandle);
    }
}


void _GraphPropertiesAppearance::OnClickedColorctrlpoint()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        pGraph->m_PointColor = m_CtrlPointColor.GetColor();
        FG_requestRedraw(m_GraphControlHandle);
    }
}


void _GraphPropertiesAppearance::OnClickedCheckVisible()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        pGraph->m_Visible = m_CheckVisible.GetCheck() == TRUE;
        FG_requestRedraw(m_GraphControlHandle);
    }
}


void _GraphPropertiesAppearance::OnCancel()
{
}


void _GraphPropertiesAppearance::OnOK()
{
}

void _GraphPropertiesAppearance::OnSelchangeComboPointSize()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        int SelectedSize = m_ComboPointSize.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        CString Temp;
        m_ComboPointSize.GetLBText(SelectedSize, Temp);
        swscanf_s(Temp, _T("%lf"), &pGraph->m_PointSize);
        FG_requestRedraw(m_GraphControlHandle);
    }
}

void _GraphPropertiesAppearance::OnBnClickedButtonRemove()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        FG_removeGraph(m_GraphControlHandle, pGraph->m_Handle);
        m_ListGraph.DeleteString(CurrentSelection);
        FG_requestRedraw(m_GraphControlHandle);
        OnSelchangeGraphList();
    }
}


void _GraphPropertiesAppearance::OnBnClickedCheckScatter()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        pGraph->m_DrawLine = !(m_CheckScatter.GetCheck() == TRUE);
        FG_requestRedraw(m_GraphControlHandle);
    }
}


void _GraphPropertiesAppearance::OnEnChangeEditGraphtitle()
{
    if(m_ListGraph.GetCount() > 0)
    {
        int CurrentSelection = m_ListGraph.GetCurSel();
        map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
        auto Iterator = pGraphs->begin();

        for(int i = 0; i < CurrentSelection; Iterator++, i++);

        _GraphData* pGraph = &Iterator->second;
        m_EditGraphTitle.GetWindowTextW(pGraph->m_Title);
        updateGraphList();
        m_ListGraph.SetCurSel(CurrentSelection);
    }
}


void _GraphPropertiesAppearance::OnBnClickedButtonExport()
{
    TCHAR Filters[] = _T("Comma separated values (*.csv)|*.csv|All Files (*.*)|*.*||");
    CFileDialog FileDlg(FALSE, _T("csv"), _T("*.csv"),
                        OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);

    // Display the file dialog. When user clicks OK, fileDlg.DoModal()
    // returns IDOK.
    if(FileDlg.DoModal() == IDOK)
    {
        CString PathName = FileDlg.GetPathName();
        FILE* pFile;
        _wfopen_s(&pFile, PathName, L"w");

        if(pFile)
        {
            int CurrentSelection = m_ListGraph.GetCurSel();
            map  <int, _GraphData>* pGraphs = m_pGraphControl->getGraphs();
            auto Iterator = pGraphs->begin();

            for(int i = 0; i < CurrentSelection; Iterator++, i++);

            _GraphData* pGraph = &Iterator->second;
            CString Csv;
            CsvIO::toCSV(pGraph, Csv);
            fwprintf_s(pFile, L"%s", Csv);
            fclose(pFile);
            CString Message;
            Message.Format(L"Exported graph data to %s.", PathName);
            MessageBox(Message);
        }
        else
        {
            MessageBox(L"Could not open the selected file for writing.");
        }
    }
    else
    {
        MessageBox(L"Invalid file selection!");
    }
}